package com.iot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iot.core.JsonHelper;
import com.iot.core.LResult;
import com.iot.core.PageData;
import com.iot.pojo.MTdevice_record;
import com.iot.pojo.MTdevice_signal;
import com.iot.service.Tdevice_recordService;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/device/record/")
public class TDevice_recordController extends BaseController {
    @Autowired
    private Tdevice_recordService service;

    @ResponseBody
    @RequestMapping("save")
    public String save(@RequestParam String bean) {
        try {

            MTdevice_record entity = JsonHelper.toBean(bean, MTdevice_record.class);
            if (entity.getDEVICE_ID() > 0) {
                service.update(entity);
            } else {
                service.insert(entity);

            }
            return LResult.Success(entity.getDEVICE_ID()).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("get")
    public String getByid(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");
            MTdevice_record bean = service.single(id);
            return LResult.Success(bean).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("del")
    public String deleteByid(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");
            service.deleteById(id);
            return LResult.Success().toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("query")
    public String queryData() {

        try {
            PageData map = getPageData();
            if (!map.containsKey("sname")) {
                map.put("sname", "");
            }
            if (!map.containsKey("sdate")) {
                map.put("sdate", "");
            }
            if (!map.containsKey("edate")) {
                map.put("edate", "");
            }

            PageHelper.startPage(Common.toInt(map.get("page")), Common.toInt(map.get("limit")));
            List<Map<String, Object>> list = service.fetchByPage(map);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            return LResult.Success(pageInfo.getTotal(), pageInfo.getList()).toJson();
        } catch (Exception ex) {
            return  LResult.Error("数据错误").toJson();
        }
    }
    @ResponseBody
    @RequestMapping("getRecordByDeviceId")
    public String getRecordByDeviceId() {
        try {
            PageData map = getPageData();

            PageHelper.startPage(Common.toInt(map.get("page")), Common.toInt(map.get("limit")));
            List<Map<String, Object>> list = service.getRecordByDeviceId(map);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            return LResult.Success(pageInfo.getTotal(), pageInfo.getList()).toJson();

        } catch (Exception ex) {
            return  LResult.Error("数据错误").toJson();
        }
    }

    @ResponseBody
    @RequestMapping("getLastDeviceSignal")
    public String getLastDeviceSignal(@RequestParam Integer deviceId) {
        try {
            //  String menu = request.getParameter("menu");
            MTdevice_signal bean = service.getLastDeviceSigal(deviceId);
            return LResult.Success(bean).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }


}
